package com.qidian.mes.tm.controller;

import com.alibaba.excel.EasyExcel;
import com.qidian.mes.pojo.Tool;
import com.qidian.mes.pojo.ToolType;
import com.qidian.mes.tm.service.ToolTypeService;
import com.qidian.mes.util.Tools;
import com.qidian.mes.util.dot.PagerDTO;
import com.qidian.mes.util.pojo.QdResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;

/**
 * @Author 陆雯城
 * @Description
 * @Time 2024-04-07 19:06
 */
@CrossOrigin
@RestController
@RequestMapping("/toolType")
public class ToolTypeController {
    @Autowired
    private ToolTypeService toolTypeService;

    @GetMapping("/export")
    public void export(ToolType toolType, @RequestParam(defaultValue = "1")int page, @RequestParam(defaultValue = "10")int limit, HttpServletResponse response) throws IOException {
        // 查询数据
        QdResult<PagerDTO<ToolType>> qdResult = toolTypeService.queryByPage(toolType, page, limit);
        List<ToolType> toolTypes = qdResult.getData().getData();
        // 导出
        try {
            // 设置contentType为excel的格式
            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
            response.setCharacterEncoding("utf-8");
            // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
            String fileName = URLEncoder.encode("工装夹具类型信息-" + System.currentTimeMillis(), "UTF-8").replaceAll("\\+", "%20");
            // 让文件以附件的形式下载
            response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
            // 这里需要设置不关闭流
            EasyExcel.write(response.getOutputStream(), ToolType.class).sheet("工装夹具类型信息").doWrite(toolTypes);
        } catch (Exception e) {
            // 重置response
            response.reset();
            response.setContentType("application/json");
            response.setCharacterEncoding("utf-8");
            response.getWriter().println(Tools.object2json(QdResult.error("下载文件失败")));
        }
    }

    @GetMapping("/queryToolTypeNames")
    public QdResult queryToolTypeNames(){
        return toolTypeService.queryToolTypeNames();
    }

    @GetMapping("/deletes")
    public QdResult deletes(Long [] toolTypeIds){
        return toolTypeService.deleteByToolTypeIds(toolTypeIds);
    }

    @GetMapping("/delete")
    public QdResult deleteByToolTypeId(Long toolTypeId){
        return toolTypeService.deleteByToolTypeId(toolTypeId);
    }

    @PostMapping("/update")
    public QdResult updateDict(@RequestBody ToolType toolType){
        return toolTypeService.updateToolType(toolType);
    }

    @GetMapping("/queryById")
    public QdResult<ToolType> queryById(Long toolTypeId){
        return toolTypeService.queryByToolTypeId(toolTypeId);
    }

    @PostMapping("/save")
    public QdResult saveToolType(@RequestBody ToolType toolType){
        System.out.println(toolType);
        return toolTypeService.saveToolType(toolType);
    }
    @GetMapping("/queryByPage") // GetMapping只能接受get请求
    public QdResult<PagerDTO<ToolType>> queryByPage(ToolType toolType, @RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "10") int limit){
        return toolTypeService.queryByPage(toolType,page,limit);
    }
}
